<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Title</title>
    <script src="js/vue@3.3.4.global.js"></script>
    <script src="js/axios.js"></script>
</head>
<body>
<!--只要是视图中要使用的变量，都必须在data模型中声明-->
    <div id="app">
        <ul>
            <!--列表渲染-->
            <li v-for="(product,index) in productList" :key="index">
                {{index}}---{{product.id}}--{{product.title}}--{{product.price}}--{{product.image}}
            </li>
        </ul>
        <button @click="get">获取远程数据</button>
        <br>
        <button @click="send">发送数组到远程</button>
        <table border="1" width="100%">
            <td>序号</td>
            <td>名称</td>
            <td>价格</td>
            <td>图片</td>
            <tr v-for="(product,index) in productList2" :key="index">
                <td>{{index+1}}</td>
                <td>{{product.title}}</td>
                <td>{{product.price}}</td>
                <td>{{product.image}}</td>
            </tr>
        </table>
    </div>
</body>
<script>
    const app = Vue.createApp({
        data(){
            return {
                productList: [], //数组
                productList2: []
            }
        },
        //使用钩子函数，程序在挂在数据时，就请求远程的数据
        mounted(){
            axios.get('http://localhost:9090/api/product/newList').then(resp=>{
                console.log(resp.data)
                //成功
                if(resp.data.code===20000){
                    this.productList = resp.data.data;
                }
            }).catch(error=>{
                console.log(error)
            });
        },
        methods:{
            /*带上token发送请求*/
            get(){
                axios.get('http://localhost:9090/api/order/list',{
                    headers: { //请求头带上token
                        Authorization: '12341242345678dfgfgnhvcdcsdcsvdsvdsvd'
                    }
                },{}).then(resp=>{
                    console.log(resp.data)
                    //成功
                    if(resp.data.code===20000){
                        //this.productList2 = resp.data.data;
                        console.log(resp.data.data)
                    }
                }).catch(error=>{
                    console.log(error)
                });
            },
            send(){
                axios.post('http://localhost:9090/array',[1,2,3]).then(resp=>{
                    console.log(resp.data)
                    //成功
                    if(resp.data.code===20000){
                        //this.productList2 = resp.data.data;
                        console.log(resp.data.data)
                    }
                }).catch(error=>{
                    console.log(error)
                });
            }
        }
    })
    app.mount('#app')
</script>

</html>